package com.yu.work.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yu.work.R;
import com.yu.work.domain.Record;
import com.yu.work.util.DateUtils;

import java.util.List;

/**
 * 记录列表适配器
 */
public class RecordListAdapter extends RecyclerView.Adapter<RecordListAdapter.RecordHolder> {

    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;
    private Context mContext;
    private List<Record> mRecords;

    public RecordListAdapter(Context context, List<Record> records) {
        mContext = context;
        mRecords = records;
    }

    public void setRecords(List<Record> records) {
        mRecords = records;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        mOnItemLongClickListener = onItemLongClickListener;
    }

    @Override
    public RecordHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View v = layoutInflater.inflate(R.layout.item_record, parent, false);
        return new RecordHolder(v);
    }

    @Override
    public void onBindViewHolder(RecordHolder holder, int position) {
        Record record = mRecords.get(position);
        holder.bind(record);
    }

    @Override
    public int getItemCount() {
        return mRecords != null ? mRecords.size() : 0;
    }


    /**
     * ViewHolder
     */
    class RecordHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

        private Record mRecord;

        //抽到的号数
        private TextView mTargetNumber;
        //备注
        private TextView mRemark;
        //创建时间
        private TextView mCreateTime;

        public RecordHolder(View itemView) {
            super(itemView);
            mTargetNumber = itemView.findViewById(R.id.tv_record_target_number);
            mRemark = itemView.findViewById(R.id.tv_record_remark);
            mCreateTime = itemView.findViewById(R.id.tv_record_create_time);
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        /**
         * 绑定数据
         *
         * @param record
         */
        public void bind(Record record) {
            mRecord = record;
            mTargetNumber.setText(String.valueOf(record.getTargetNumber()));
            mRemark.setText(record.getRemark());
            mCreateTime.setText(DateUtils.format(record.getCreateTime()));
        }

        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(mRecord);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (mOnItemLongClickListener != null) {
                mOnItemLongClickListener.onItemLongClick(mRecord);
            }
            return false;
        }
    }

    /**
     * 回调接口
     */
    public interface OnItemClickListener {

        /**
         * 设置 RecyclerView Item 项的点击事件
         *
         * @param record
         */
        void onItemClick(Record record);
    }

    public interface OnItemLongClickListener {

        /**
         * 长按事件
         *
         * @param record
         */
        void onItemLongClick(Record record);
    }
}
